package com.test.chinamobileai.config;


import com.test.chinamobileai.constant.Constant;
import com.test.chinamobileai.embedding.RuleDateEmbedding;
import com.test.chinamobileai.tools.WeatherTools;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.client.advisor.api.Advisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.InMemoryChatMemory;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.vectorstore.SimpleVectorStore;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * @author L9629
 */
@Configuration
public class AiConfig {
    /**
     * 构造一个ChatClient对象,放到Spring容器中
     */
    @Bean
    public ChatClient chatClient(ChatClient.Builder chatClient,@Qualifier("simpleLoggerAdvisor") Advisor simpleLoggerAdvisor,
                                 @Qualifier("messageChatMemoryAdvisor") Advisor messageChatMemoryAdvisor, WeatherTools weatherTools){
        return chatClient
                .defaultSystem(Constant.SYSTEM_ROLE)
                .defaultAdvisors(simpleLoggerAdvisor,messageChatMemoryAdvisor)
                .defaultTools(weatherTools)
                .build();
    }

    /**
     * 打印日志
     * @return
     */
    @Bean
    public Advisor simpleLoggerAdvisor(){
        return new SimpleLoggerAdvisor();
    }

    /**
     * 创建聊天记忆管理器
     */
    @Bean
    public ChatMemory chatMemory(){
        return new InMemoryChatMemory();
    }
    /**
     *  记忆聊天
     */
    @Bean
    public Advisor messageChatMemoryAdvisor(ChatMemory chatMemory){
        return new MessageChatMemoryAdvisor(chatMemory);
    }

    /**
     * 向量模型
     * @param embeddingModel
     * @return
     */
    @Bean
    public VectorStore vectorStore(EmbeddingModel embeddingModel) {
        return SimpleVectorStore.builder(embeddingModel).build();
    }

}
